package org.xtext.runtime.formula;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;
import org.xtext.runtime.util.ILogicVisitor;


public class IMPLIES implements IFormula {
	private IFormula context;
	private Predicate result;
	private Set<Variable> set = new HashSet<Variable>();
	
	
	public IMPLIES(IFormula context, Predicate result) {
		this.context = context;
		this.result = result;

		set.addAll(context.variables());
		set.addAll(result.variables());
	}
	
	public IFormula context() {
		return context;
	}
	
	public Predicate result() {
		return result;
	}

	public String toString() {
		return context + "=>" + result;
	}

	@Override
	public Set<Variable> variables() {
		return set;
	}

	@Override
	public IFormula apply(Bindings bindings) {
		context.apply(bindings);
		result.apply(bindings);
		return this;
		
	}

	public IMPLIES rename(String prefix) {
		Map<String, Variable> vars = new HashMap<String, Variable>();
		IMPLIES implies = this.copy(vars);
		for (Variable variable : vars.values()) {
			variable.applyRenaming(prefix);
		}
		return implies;
	}
	
	@Override
	public IMPLIES copy() {
		Map<String, Variable> vars = new HashMap<String, Variable>();
		return new IMPLIES(context.copy(vars), (Predicate) result.copy(vars));
	}
	
	@Override
	public IMPLIES copy(Map<String, Variable> vars) {
		return new IMPLIES(context.copy(vars), (Predicate) result.copy(vars));
	}

	@Override
	public boolean hasFreeVariables() {
		return context.hasFreeVariables() || result.hasFreeVariables();
	}

	@Override
	public Object accept(ILogicVisitor visitor) {
		return visitor.visit(this);
	}
}
